package aa;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import org.opencv.imgproc.Imgproc;

import com.attilax.img.HSV;
import com.attilax.img.ImagePHash;
import com.attilax.img.imgUtil;
import com.attilax.img.imgx;
import com.attilax.img.other.CantFindMatch;
import com.attilax.img.other.ColorUtil;
import com.attilax.img.util.OpencvUtil;
import com.attilax.io.dirx;
import com.attilax.io.filex;
import com.attilax.io.pathx;
import com.attilax.json.AtiJson;
import com.attilax.log.LogUtil;
import com.google.common.collect.Maps;

public class Wechat_contact_ui_recgr_bycolor {
	public static int cnt;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		LogUtil.redirectSystemOutAndErrToLog_slf4j();
		OpencvUtil.ini();
		String tmplt_dir = pathx.appPathV2() + "\\wechat_contact_ui_recg" + "";
		// D:\0workspace\atiplat_img\
		String dir = "D:\\00wechatrecg";// List<String> li=
	     dir = "D:\\ati\\r5 cpbek screen Screenshots";
		String tmp_pic = tmplt_dir + "\\btn.png";
		System.out.println("tmp pic:" + tmp_pic);
	//	BufferedImage tmplt_bi = imgUtil.toBufferedImage(tmp_pic);

		dirx.traveV3(dir, new Function<String, Object>() {

			@Override
			public Object apply(String f) {
				cnt++;
				try {
					BufferedImage fi = imgx.toImg(f);
					Rectangle rect = new Rectangle(0, fi.getHeight() / 2, fi.getWidth(), fi.getHeight());
					BufferedImage selectedAreaImg = new imgx().cutImage_retImg(f, rect);
					HSV low = new HSV().setHsv100mode(110, 65, 47);
					HSV hi = new HSV().setHsv100mode(130, 105, 87);
					int colorCounts = ColorUtil.colorCounts(selectedAreaImg, hi, low);
					float pct = (float) colorCounts * 100 / (float) (selectedAreaImg.getWidth() * selectedAreaImg.getHeight());

					String name = filex.getFileName_mainname(f);
					imgx.save_overwrite(selectedAreaImg, "D:\\00dbg_out\\" + name + "_selected.jpg");

					Map m = Maps.newConcurrentMap();
					m.put("f", f);
					m.put("tmp pic", tmp_pic);

					m.put("pct", pct);
					m.put("cnt", cnt);
					System.out.println(">>>>>>" + AtiJson.toJson(m)+"<<<<<<atienter");

					// BufferedImage nowpic=imgx.toImg(f);
					// BufferedImage dbg=imgx.toImg(f);
					// try {
					// org.opencv.core.Point p= OpencvUtil.matchTemplate(nowpic,
					// bi, dbg, Imgproc.TM_SQDIFF);
					// System.out.println(f);
					// } catch (CantFindMatch | IOException e) {
					//
					// }

				} catch (Exception e) {
					e.printStackTrace();
				}
				return null;
			}
		});

		System.out.println("--finish");

	}

}
